#include "UtilityPCH.h"

#include "UT_SqliteResult.h"
#include "UT_StringHelper.h"

using namespace Utility;

//------------------------------------------------------------------------
//UT_SQLite_Result
//------------------------------------------------------------------------
UT_SQLite_Result::UT_SQLite_Result (void):
result_ (NULL),
row_count_ (0),
col_count_ (0),
current_row_ (0),
current_col_ (0)
{

}
//------------------------------------------------------------------------
UT_SQLite_Result::~UT_SQLite_Result (void)
{
	if (NULL != result_)
	{
		sqlite3_free_table (result_);
		result_ = NULL;
	}
}
//------------------------------------------------------------------------
bool UT_SQLite_Result::IsNULL (void)
{
	if(NULL == result_)
	{
		return true;
	}

	return false;
}
//------------------------------------------------------------------------
void UT_SQLite_Result::SetResultData (char** result, size_t row_count, size_t col_count)
{
	if (NULL != result_)
	{
		FreeResult();
	}

	result_ = result;
	row_count_ = row_count;
	col_count_ = col_count;
	current_row_ = 0;
	current_col_ = 0;
}
//------------------------------------------------------------------------
void UT_SQLite_Result::FreeResult (void)
{
	if (NULL != result_)
	{
		sqlite3_free_table (result_);
		result_ = NULL;
		row_count_ = 0;
		col_count_ = 0;
		current_row_ = 0;
		current_col_ = 0;
	}
}
//------------------------------------------------------------------------
bool UT_SQLite_Result::FetchNexRow (void)
{
	if (NULL == result_)
	{
		return false;
	}

	if (current_row_ < row_count_)
	{
		++current_row_;
		current_col_ = 0;
		return true;
	}

	return false;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (int8_t& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = UT_StringHelper::StringToInt32((result_[current_index_]));
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (int16_t& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = (int16_t)UT_StringHelper::StringToInt32(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (int32_t& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = (int32_t)UT_StringHelper::StringToInt32(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (int64_t& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = (int64_t)UT_StringHelper::StringToInt64(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (uint8_t& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = (uint8_t)UT_StringHelper::StringToInt32(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (uint16_t& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = (uint16_t)UT_StringHelper::StringToInt32(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (uint32_t& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = (uint32_t)UT_StringHelper::StringToInt64(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (uint64_t& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = (uint64_t)UT_StringHelper::StringToInt64(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (float& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = (float)UT_StringHelper::StringToDouble(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (double& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val = (double)UT_StringHelper::StringToDouble(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::operator >> (StringType& val)
{
	if (current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_])
		{
			val.assign(result_[current_index_]);
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::GetString (CharType* buff, size_t buff_len)
{
	if (NULL != buff && current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_] && buff_len > strlen(result_[current_index_]))
		{
			STRNCPY(buff,result_[current_index_],sizeof(buff));
		}

		++current_col_;
	}

	return *this;
}
//------------------------------------------------------------------------
UT_SQLite_Result& UT_SQLite_Result::GetBuff (uint8_t* buff, size_t buff_len)
{
	if (NULL != buff && current_row_ <= row_count_ && current_col_ < col_count_)
	{
		size_t current_index_ = (current_row_ * col_count_) + current_col_;
		if (NULL != result_[current_index_] && buff_len > strlen(result_[current_index_]))
		{
			memcpy(buff,result_[current_index_],sizeof(buff));
		}

		++current_col_;
	}

	return *this;
}



